<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/13 0013
 * Time: 15:33
 */

namespace app\admin\controller;


use think\Request;

class Contactus extends Base
{
    protected $_Message;
    protected $_product;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Message = new \app\model\model\Contact();
        $this->_product = new \app\model\model\Product();
    }
    public function lists(){
        $productlist = $this->_product->getListSelect(['flag'=>1]);
        $this->assign('productlist',$productlist);
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        $type = input('type');
        $protype = input('protype');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        if($type){
            $where['flag'] = $type;
        }
        if($protype){
            $where['product_id'] = $protype;
        }
        $where['type'] = ['<>',''];
        $data = $this->_Message->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            switch ($v['type'])
            {
                case 1:
                    $v['type']="联系我们页";
                    $v['product'] = '联系我们页';
                    break;
                case 2:
                    $v['type']="下载页";
                    $v['product'] = '下载页';
                    break;
                case 3:
                    $productdata = $this->_product->getOne($v['product_id']);
                    $v['type']="ID为".$v['product_id']."的产品详情页";
                    $v['product'] =$productdata['name'];
                    break;
                case 10:
                    $v['type']="底部合作意向";
                    $v['product'] = '底部合作意向';
                    break;
                default:
                    $v['type']="未知";
                    $v['product'] = '未知';


            }
            switch ($v['status'])
            {
                case 1:
                    $v['status']="未分配";
                    break;
                case 2:
                    $v['status']="已分配，未处理";
                    break;
                default:
                    $v['status']="已处理";
            }
            switch ($v['flag'])
            {
                case 1:
                    $v['flag']="审核完毕";
                    break;
                case 2:
                    $v['flag']="未审核";
                    break;
                default:
                    $v['flag']="无效";
            }

        }
        $list = $data->toArray();


        echo json_echo($list);
    }
    /**
     * 分配
     * */
    public function reply(){

        if(!Request::instance()->post()){
            $salesman = new \app\model\model\Salesman();
            $slist = $salesman->getListSelect();
            $this->assign('slist',$slist);
            $data = $this->_Message->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            if(input('status')==3){
                $data = [
                    'status'=>input('status'),

                ];
                $re = $this->_Message->up(['id'=>input('id')],$data);
                if($re){
                    return json_echo_msg(200,'分配成功');
                }else{
                    return json_echo_msg(500,'分配失败');
                }
            }else{
                $data = [
                    'status'=>input('status'),
                    'salesman_id'=>input('salesman_id'),
                    'copy_to'=>input('copy_to'),
                ];
                $re = $this->_Message->up(['id'=>input('id')],$data);

                if($re){
                    $salesman = new \app\model\model\Salesman();
                    $data1 = $salesman->getOne(input('salesman_id'));
                     $url = WEB_URL.'admin';
                     $res = email_send($data1['email'],$data['copy_to'],true,"欧锐激光--通知邮件","有一条新的用户询盘需要处理，请<a href=".$url.">登录后台</a>查看！");
                     if($res){
                        return json_echo_msg(200,'分配成功');
                    }
                }else{
                    return json_echo_msg(500,'分配失败');
                }
            }
        }
    }
    /**
     * 审核
     * */
    public function audit(){

        $re = $this->_Message->up(['id'=>input('id')],['flag'=>1]);
        if($re){
            return json_echo_msg(200,'审核成功');
        }else{
            return json_echo_msg(500,'已审核');
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Message->getOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){
        $re = $this->_Message->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAll(){
        $re = $this->_Message->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

}